<?php
class Components_Calendar_Controller
{
	const TEMPLATE_NAME_DESIGN	= 'design.inc';

	const KEY_CSSCLASS		= 'CSSCLASS';
	const KEY_DAYNUM		= 'DAYNUM';
	const KEY_YOBI			= 'YOBI';
	const KEY_SCHEDHTML		= 'SCHEDHTML';
	const KEY_HOLIDAYNAME	= 'HOLIDAYNAME';

	const CSS_CLASSNAME_HOLIDAY		= 'holiday';
	const CSS_CLASSNAME_SATURDAY	= 'saturday';
	const CSS_CLASSNAME_TODAY		= 'today';
	const CSS_CLASSNAME_WEEKDAY		= 'bgf';

	const YOBI_SUNDAY   	= 0;
	const YOBI_MONDAY		= 1;
	const YOBI_TUESDAY		= 2;
	const YOBI_WEDNESDAY	= 3;
	const YOBI_THURSDAY		= 4;
	const YOBI_FRIDAY		= 5;
	const YOBI_SATURDAY		= 6;

	protected $_aryDayList;
	protected $_aryHolidayList;
	protected $_arySchedList;
	protected $_strBasePath;
	protected $_bMgr;

	public function __construct()
	{
		$this->_aryDayList = array();
		$this->_aryHolidayList = array();
		$this->_arySchedList = array();
		$this->_strBasePath = '';
		$this->_bMgr = '';
	}

	public function setBasePath( $strBasePath )
	{
		$this->_strBasePath = $strBasePath;
	}

	public function setMgr( $bMgr )
	{
		if ( $bMgr == TRUE ) {
			$this->_bMgr = '1';
		}
	}

	/**
	 * スケジュール情報を追加する
	 * @param $iYear
	 * @param $iMonth
	 * @param $iDay
	 * @param $strSchedInfo
	 */
	public function addSchedInfo( $iYear, $iMonth, $iDay, $strSchedInfo )
	{
		$this->_arySchedList[$iYear.'_'.$iMonth.'_'.$iDay][] = $strSchedInfo;
	}

	/**
	 * 指定年月日のスケジュール情報を取得する
	 * @param unknown_type $iYear
	 * @param unknown_type $iMonth
	 * @param unknown_type $iDay
	 */
	public function getSchedInfo( $iYear, $iMonth, $iDay )
	{
		if ( is_array( $this->_arySchedList ) ) {
			$iMonth = sprintf( '%d', $iMonth );
			$iDay = sprintf( '%d', $iDay );
			if ( array_key_exists( $iYear.'_'.$iMonth.'_'.$iDay, $this->_arySchedList ) ) {
				return $this->_arySchedList[$iYear.'_'.$iMonth.'_'.$iDay];
			}
		}
		$aryNull = array();
		return $aryNull;
	}

	/**
	 * 祝日情報を追加する
	 * @param unknown_type $iYear
	 * @param unknown_type $iMonth
	 * @param unknown_type $iDay
	 * @param unknown_type $strHolidayName
	 */
	public function addHolidayInfo( $iYear, $iMonth, $iDay, $strHolidayName )
	{
		$this->_aryHolidayList[$iYear.'_'.$iMonth.'_'.$iDay] = $strHolidayName;
	}

	/**
	 * 指定年月日が祝日の場合、祝日名称を返す
	 * @param unknown_type $iYear
	 * @param unknown_type $iMonth
	 * @param unknown_type $iDay
	 * @return string|boolean
	 */
	public function isHoliday( $iYear, $iMonth, $iDay ) {
		if ( is_array( $this->_aryHolidayList ) ) {
			$iMonth = sprintf( '%d', $iMonth );
			$iDay = sprintf( '%d', $iDay );
			if ( array_key_exists( $iYear.'_'.$iMonth.'_'.$iDay, $this->_aryHolidayList ) ) {
				return $this->_aryHolidayList[$iYear.'_'.$iMonth.'_'.$iDay];
			}
		}
		return FALSE;
	}

	/**
	 * 指定年月日が祝日の場合、祝日名称を返す
	 * @param unknown_type $iYear
	 * @param unknown_type $iMonth
	 * @param unknown_type $iDay
	 */
	public function getHolidayName( $iYear, $iMonth, $iDay )
	{
		$strHolidayName = $this->isHoliday( $iYear, $iMonth, $iDay );
		if ( $strHolidayName === FALSE ) {
			return '';
		}
		return $strHolidayName;
	}

	public function addDayInfo( $iDayNum, $iDayOfWeek, $bHoliday, $strHolidayName, $strHtmlSched )
	{
		$strCssClassName = self::CSS_CLASSNAME_WEEKDAY;
		if ( $iDayOfWeek == self::YOBI_SATURDAY ) {
			$strCssClassName = self::CSS_CLASSNAME_SATURDAY;
		}
		if ( $iDayOfWeek == self::YOBI_SUNDAY ) {
			$strCssClassName = self::CSS_CLASSNAME_HOLIDAY;
		}
		if ( $bHoliday == TRUE ) {
			$strCssClassName = self::CSS_CLASSNAME_HOLIDAY;
		}
		$this->_aryDayList[$iDayNum] = array(
		self::KEY_YOBI			=> $iDayOfWeek,
		self::KEY_HOLIDAYNAME	=> $strHolidayName,
		self::KEY_SCHEDHTML		=> $strHtmlSched,
		);
	}

	/**
	 * 指定年月日の曜日番号を求める
	 * @param $iYear
	 * @param $iMonth
	 * @param $iDay
	 */
	public static function getYobiNum( $iYear, $iMonth, $iDay )
	{
		$dayOfWeek = date("w", mktime(0, 0, 0, $iMonth, $iDay, $iYear));
		return $dayOfWeek;
	}

	/**
	 * 月末日を求める
	 * @param $year
	 * @param $month
	 * @see http://www.hoge256.net/2007/08/63.html
	 */
	public static function getMonthEndDay($year, $month) {
		//mktime関数で日付を0にすると前月の末日を指定したことになります
		//$month + 1 をしていますが、結果13月のような値になっても自動で補正されます
		$dt = mktime(0, 0, 0, $month + 1, 0, $year);
		return date("d", $dt);
	}

	/**
	 * 指定年月の週数を算出する
	 * @param $year
	 * @param $month
	 * @param $startyobi
	 */
	public static function getWeekNum($year, $month, $startyobi) {
		$iMonthEndDay = $this->getMonthEndDay( $year, $month );
		$iWeekNum = 1;
		$i = 1;
		while ( $i <= $iMonthEndDay ) {
			$yobi = date('w', mktime(0,0,0,$month,$i,$year));
			if ( $yobi == $startyobi ) {
				if ( $i > 1 ) {
					$iWeekNum ++;
				}
			}
			$i ++;
		}
		return $iWeekNum;
	}

	/**
	 * 任意の年月の第n曜日の日付を求める関数
	 * @param $year 年
	 * @param $month 月
	 * @param $number 何番目の曜日か、第1曜日なら1。第3曜日なら3
	 * @param $dayOfWeek 求めたい曜日。0-6までの数字で曜日の日?土を指定する
	 */
	public static function getWhatDayOfWeek($year, $month, $number, $dayOfWeek) {
	    $firstDayOfWeek = date("w", mktime(0, 0, 0, $month, 1, $year));//指定した年月の1日の曜日を取得
	    $day = $dayOfWeek - $firstDayOfWeek + 1;
	    if($day <= 0) {
	    	$day += 7;//1週間を足す
	    }
	    $dt = mktime(0, 0, 0, $month, $day, $year);
	    $dt += (86400 * 7 * ($number - 1));//n曜日まで1週間を足し込み
	    return date("j", $dt);
	}

	public static function getNextYobiNum( $iYobi )
	{
		if ( $iYobi == self::YOBI_SATURDAY ){
			return self::YOBI_SUNDAY;
		} else {
			$iYobi ++;
			return $iYobi;
		}
	}

	public static function getBeforYobiNum ( $iYobi )
	{
		if ( $iYobi == self::YOBI_SUNDAY ) {
			return self::YOBI_SATURDAY;
		} else {
			$iYobi --;
			return $iYobi;
		}
	}
}